package product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import com.feign.valid.AddGroup;
import com.feign.valid.UpdateGroup;
import com.feign.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import product.domain.Brand;
import product.service.BrandService;
import org.springframework.web.bind.annotation.*;
import product.utils.GetUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 品牌(Brand)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:17
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private GetUtils getUtils;

    @GetMapping("list")
    public R getBrandList(HttpServletRequest request) {
        Map<String, Object> params = getUtils.getParamsWithMapByRequest(request);
        Page<Brand> brandPage = brandService.queryPage(params);
        return R.ok().put("data", brandPage);
    }

    /**
     * 删除品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R removeCategory(@RequestBody Long[] ids) {
        boolean result = brandService.removeBatchByIds(List.of(ids));
        return result ? R.ok("删除品牌成功") : R.error("删除品牌失败");
    }


    /**
     * 增加品牌
     *
     * @param brand
     * @return
     */
    @PostMapping("save")
    public R save(@Validated({AddGroup.class}) @RequestBody Brand brand) {
        boolean result = brandService.save(brand);
        return result ? R.ok("增加品牌成功") : R.error("增加品牌失败");
    }

    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @PutMapping("update")
    public R edit(@Validated({UpdateGroup.class}) @RequestBody Brand brand) {
        boolean result = brandService.updateDetail(brand);
        return result ? R.ok("修改品牌成功") : R.error("修改品牌失败");
    }


    /**
     * 获取单个品牌的数据
     *
     * @param catId
     * @return
     */
    @GetMapping("info/{catId}")
    public R info(@PathVariable Long catId) {
        Brand brand = brandService.getById(catId);
        return R.ok().put("data", brand);
    }

    /**
     * 修改状态
     *
     * @param brand
     * @return
     */
    @PutMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody Brand brand) {
        boolean result = brandService.updateById(brand);
        return result ? R.ok("状态更新成功") : R.error("状态更新失败");
    }

}

